package com.common.util;


import cn.hutool.core.util.DesensitizedUtil;
import com.common.constant.GrepConstant;
import com.common.enums.error.ResultEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: stefanie-zy
 * @time: 2024/1/25-10:17
 * @description: 正则表达式工具类
 */
@Slf4j
@Component
@Schema(name = "工具类-正则表达式")
public class GrepUtil {

    @Operation(summary = "是否全是中文", description = "true:全是中文；false:不全是")
    public static boolean isAllChinese(String str) {
        return isCommon(str, GrepConstant.CHINESE);
    }

    @Operation(summary = "是否包含中文", description = "true:包含；false:不包含")
    public static boolean isContainsChinese(String str) {
        boolean result = false;
        try {
            if (str != null) {
                Pattern pattern = Pattern.compile(GrepConstant.CHINESE);
                Matcher matcher = pattern.matcher(str);
                result = matcher.find();
            }
        } catch (Exception e) {
            log.error(ResultEnum.U_GREP_ERROR.getMessage());
        }
        return result;
    }

    @Operation(summary = "是否为手机号", description = "true:是；false:不是")
    public static boolean isPhoneNumber(String str) {
        return isCommon(str, GrepConstant.PHONE_NUMBER);
    }

    @Operation(summary = "是否为邮箱", description = "true:是；false:不是")
    public static boolean isEmail(String str) {
        return isCommon(str, GrepConstant.EMAIL);
    }

    @Operation(summary = "是否为身份证", description = "true:是；false:不是")
    public static boolean isIdCard(String str) {
        return isCommon(str, GrepConstant.ID_CARD);
    }

    @Operation(summary = "手机号脱敏", description = "默认展示前3位后4位")
    public static String phoneNumberHide(String phoneNumber) {
        return DesensitizedUtil.mobilePhone(phoneNumber);
    }

    @Operation(summary = "邮箱脱敏")
    public static String emailHide(String email) {
        return DesensitizedUtil.email(email);
    }

    @Operation(summary = "身份证脱敏", description = "默认展示前3位后4位")
    public static String idCardHide(String idCard) {
        return DesensitizedUtil.idCardNum(idCard, 3, 4);
    }

    @Operation(summary = "地址脱敏", description = "默认隐藏后6位")
    public static String addressHide(String address) {
        return DesensitizedUtil.address(address, 6);
    }

    @Operation(summary = "公共规则校验，只支持matcher.matches()方法", description = "true:一致；false:不一致")
    private static boolean isCommon(String str, String grepStr) {
        boolean result = false;
        try {
            if (!StringUtils.isEmpty(str) && !StringUtils.isEmpty(grepStr)) {
                Pattern pattern = Pattern.compile(grepStr);
                Matcher matcher = pattern.matcher(str);
                result = matcher.matches();
            }
        } catch (Exception e) {
            log.error(ResultEnum.U_GREP_ERROR.getMessage());
        }
        return result;
    }
}
